﻿using GalaSoft.MvvmLight.Threading;
using GalaSoft.MvvmLight.Views;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace msap.Common
{
    public class WPFDialogService:IDialogService
    {
        public Task ShowError(string message, string title, string buttonText, Action afterHideCallback)
        {
            return Task.Run(() =>
            {
                DispatcherHelper.UIDispatcher.Invoke(() =>
                {
                    ErrorWindow window = new ErrorWindow();
                    window.ShowDialog(message, title, buttonText);
                    if (afterHideCallback != null)
                        afterHideCallback.Invoke();
                });
            });
        }

        public Task ShowError(Exception error, string title, string buttonText, Action afterHideCallback)
        {
            return Task.Run(() =>
                DispatcherHelper.UIDispatcher.Invoke(() =>
                {
                    ErrorWindow window = new ErrorWindow();
                    window.ShowDialog(error.Message, title, buttonText);
                    if (afterHideCallback != null)
                        afterHideCallback.Invoke();
                }));
        }

        public Task ShowMessage(string message, string title)
        {
            return Task.Run(() =>
                DispatcherHelper.UIDispatcher.Invoke(() =>
                {
                    MessageWindow window = new MessageWindow();
                    window.ShowDialog(message, title, null);
                }));
        }

        public Task ShowMessage(string message, string title, string buttonText, Action afterHideCallback)
        {
            return Task.Run(() =>
                DispatcherHelper.UIDispatcher.Invoke(() =>
                {
                    MessageWindow window = new MessageWindow();
                    window.ShowDialog(message, title, buttonText);
                    if (afterHideCallback != null)
                        afterHideCallback.Invoke();
                }));
        }

        public async Task<bool> ShowMessage(string message, string title, string buttonConfirmText, string buttonCancelText, Action<bool> afterHideCallback)
        {
            return await Task.Run(() =>
            {
                bool result = false;
                DispatcherHelper.UIDispatcher.Invoke(() =>
                {
                    QuestionWindow window = new QuestionWindow();
                    window.ShowDialog(message, title, buttonConfirmText, buttonCancelText);
                    result = window.DialogResult.Value;
                    if (afterHideCallback != null)
                        afterHideCallback(result);
                });
                return result;
            });
        }

        public Task ShowMessageBox(string message, string title)
        {
            throw new NotImplementedException();
        }
    }
}
